package com.augurit.gzcgwsp.excelimportexport.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.augurit.gzcgwsp.excelimportexport.dao.YwRqQyjyxkGdyjgqjgdjbDao;
import com.augurit.gzcgwsp.excelimportexport.entity.YwRqQyjyxkGdyjgqjgdjb;
import com.augurit.gzcgwsp.excelimportexport.service.IYwRqQyjyxkGdyjgqjgdjbService;
import com.augurit.gzcgwsp.rqqyjyxk.entity.YwRqQyjyxkFinalGdyjgqjgdjb;
import com.augurit.gzcgwsp.rqqyjyxk.service.IYwRqQyjyxkFinalGdyjgqjgdjbService;

@Service
@Transactional
public class YwRqQyjyxkGdyjgqjgdjbServiceImpl implements
		IYwRqQyjyxkGdyjgqjgdjbService {
	@Autowired
	private YwRqQyjyxkGdyjgqjgdjbDao ywRqQyjyxkGdyjgqjgdjbDao;
	   @Autowired
    private IYwRqQyjyxkFinalGdyjgqjgdjbService ywRqQyjyxkFinalGdyjgqjgdjbService;
	/**
	 * 保存 股东以及股权结构登记表
	 * 
	 * @param ywRqQyjyxkGdyjgqjgdjb
	 */
	public void save(YwRqQyjyxkGdyjgqjgdjb ywRqQyjyxkGdyjgqjgdjb) {
		ywRqQyjyxkGdyjgqjgdjbDao.save(ywRqQyjyxkGdyjgqjgdjb);
	}
	
	/**
	 * 根据id获取  股东以及股权结构登记表
	 * @param id
	 * @return
	 */
	private List<YwRqQyjyxkGdyjgqjgdjb> getListByQyid(Long qyid){
	    String hql = "from YwRqQyjyxkGdyjgqjgdjb t where t.qyid='" + qyid + "'";
        List<YwRqQyjyxkGdyjgqjgdjb> entityList = ywRqQyjyxkGdyjgqjgdjbDao.find(hql);
        return entityList;
	}

	/**
	 * 解析 股东以及股权结构登记表
	 * 
	 * @param hssfsheet
	 * @param acceptNo
	 * @param qyid
	 * @param username
	 * @return
	 */
	public void parseGdyjgqjgdjb(HSSFSheet hssfsheet,
			String acceptNo, Long qyid, String username) {
		String xh = null;// 序号
		String qygdmc = null;// 企业股东名称
		String gfszbl = null;// 股份所占比例
		

			String hql = "from YwRqQyjyxkGdyjgqjgdjb t where t.qyid = "+qyid+"";
			List<YwRqQyjyxkGdyjgqjgdjb> list=ywRqQyjyxkGdyjgqjgdjbDao.find(hql);
			for(int i=0;i<list.size();i++)
			{
				ywRqQyjyxkGdyjgqjgdjbDao.delete(list.get(i));
			}
		
		try {

			HSSFRow hssfrow = hssfsheet.getRow(0);// 第一行
			// 遍历该行所有的行,j表示行数 getPhysicalNumberOfRows行的总数
			for (int j = 0; j < hssfsheet.getPhysicalNumberOfRows(); j++) {

				hssfrow = hssfsheet.getRow(j);
				// 判断是否还存在需要导入的数据
				if (hssfrow == null) {
					// System.out.println("这里已没有数据，在第" + i + "列,第" + j + "行");
					break;
				}
				
				if(j >= 3){
					if (hssfrow.getCell((short) 0) == null) {
						xh = "";
					} else if (hssfrow.getCell((short) 0).getCellType() == 0) {
						xh = new Double(hssfrow.getCell((short) 0)
								.getNumericCellValue()).toString();
					} else {// 如果EXCEL表格中的数据类型为字符串型
						xh = hssfrow.getCell((short) 0).getStringCellValue()
								.trim();
					}
					
					if (hssfrow.getCell((short) 1) == null) {
						qygdmc = "";
					} else if (hssfrow.getCell((short) 1).getCellType() == 0) {
						qygdmc = new Double(hssfrow.getCell((short) 1)
								.getNumericCellValue()).toString();
					} else {// 如果EXCEL表格中的数据类型为字符串型
						qygdmc = hssfrow.getCell((short) 1).getStringCellValue()
								.trim();
					}
					
					if (hssfrow.getCell((short) 2) == null) {
						gfszbl = "";
					} else if (hssfrow.getCell((short) 2).getCellType() == 0) {
						gfszbl = new Double(hssfrow.getCell((short) 2)
								.getNumericCellValue()).toString();
					} else {// 如果EXCEL表格中的数据类型为字符串型
						gfszbl = hssfrow.getCell((short) 2).getStringCellValue()
								.trim();
					}
					if(StringUtils.isEmpty(qygdmc) && StringUtils.isEmpty(gfszbl)){
						break;
					}else{
						YwRqQyjyxkGdyjgqjgdjb ywRqQyjyxkGdyjgqjgdjb = new YwRqQyjyxkGdyjgqjgdjb();
						ywRqQyjyxkGdyjgqjgdjb.setQyid(qyid);
						ywRqQyjyxkGdyjgqjgdjb.setAcceptNo(acceptNo);
						ywRqQyjyxkGdyjgqjgdjb.setGfszbl(gfszbl);
						ywRqQyjyxkGdyjgqjgdjb.setQygdmc(qygdmc);
						ywRqQyjyxkGdyjgqjgdjb.setUploadDate(new Date());
						ywRqQyjyxkGdyjgqjgdjb.setXh(xh);
						ywRqQyjyxkGdyjgqjgdjb.setUserName(username);
						this.save(ywRqQyjyxkGdyjgqjgdjb);
					}
					
				}
				
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
     * 导出 股东以及股权结构登记表
     * 
     * @param hssfsheet
     * @param acceptNo
     * @param qyid
     * @param username
     * @return
     */
    public HSSFSheet expGdyjgqjgdjb(HSSFSheet hssfsheet,
            String acceptNo, Long qyid, String username,HSSFCellStyle cs,String exptype) {
        if("qyxx".equals(exptype)){//导出原始数据
            List<YwRqQyjyxkFinalGdyjgqjgdjb> entityList = ywRqQyjyxkFinalGdyjgqjgdjbService.getGdInfoByQyid(qyid);
            if(entityList == null || entityList.size() <= 0) return null;
            if(entityList!=null &&entityList.size()>0) {
                try {
                    /**
                     * i代表生成的列表行，
                     */
                    for(int i = 3, j = 0; j< entityList.size(); i++){
                        if(i < 3) continue;
                        YwRqQyjyxkFinalGdyjgqjgdjb entity = entityList.get(j);
                        if(entity != null){
                            HSSFRow row = hssfsheet.createRow(i);// 创建一个行对象
                            HSSFCell cell0 = row.createCell(0);// 在本行的第一个单元格中写入数据
                            HSSFRichTextString text0 = new HSSFRichTextString(entity.getXh()==null?"":entity.getXh());// 声明文本内容
                            cell0.setCellValue(text0);// 将文本对象加入单元格
                            cell0.setCellStyle(cs); 
                            
                            HSSFCell cell1 = row.createCell(1);// 在本行的第一个单元格中写入数据
                            HSSFRichTextString text1 = new HSSFRichTextString(entity.getQygdmc()==null?"":entity.getQygdmc());// 声明文本内容
                            cell1.setCellValue(text1);// 将文本对象加入单元格
                            cell1.setCellStyle(cs); 
                            
                            HSSFCell cell2 = row.createCell(2);// 在本行的第一个单元格中写入数据
                            HSSFRichTextString text2 = new HSSFRichTextString(entity.getGfszbl()==null?"":entity.getGfszbl());// 声明文本内容
                            cell2.setCellValue(text2);// 将文本对象加入单元格
                            cell2.setCellStyle(cs); 
                        }
                        j++;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
        }
       }else{
           List<YwRqQyjyxkGdyjgqjgdjb> entityList = this.getListByQyid(qyid);
           if(entityList == null || entityList.size() <= 0) return null;
           if(entityList!=null) {
           try {
               /**
                * i代表生成的列表行，
                */
               for(int i = 3, j = 0; j< entityList.size(); i++){
                   if(i < 3) continue;
                   
                   YwRqQyjyxkGdyjgqjgdjb entity = entityList.get(j);
                   if(entity != null){
                       HSSFRow row = hssfsheet.createRow(i);// 创建一个行对象
                       HSSFCell cell0 = row.createCell(0);// 在本行的第一个单元格中写入数据
                       HSSFRichTextString text0 = new HSSFRichTextString(entity.getXh()==null?"":entity.getXh());// 声明文本内容
                       cell0.setCellValue(text0);// 将文本对象加入单元格
                       cell0.setCellStyle(cs); 
                       
                       HSSFCell cell1 = row.createCell(1);// 在本行的第一个单元格中写入数据
                       HSSFRichTextString text1 = new HSSFRichTextString(entity.getQygdmc()==null?"":entity.getQygdmc());// 声明文本内容
                       cell1.setCellValue(text1);// 将文本对象加入单元格
                       cell1.setCellStyle(cs); 
                       
                       HSSFCell cell2 = row.createCell(2);// 在本行的第一个单元格中写入数据
                       HSSFRichTextString text2 = new HSSFRichTextString(entity.getGfszbl()==null?"":entity.getGfszbl());// 声明文本内容
                       cell2.setCellValue(text2);// 将文本对象加入单元格
                       cell2.setCellStyle(cs); 
                   }
                   j++;
               }
           } catch (Exception e) {
               e.printStackTrace();
           }
          }
       }
        return hssfsheet;
    }

    /*
     * (non-Javadoc)
     * @see com.augurit.gzcgwsp.excelimportexport.service.IYwRqQyjyxkGdyjgqjgdjbService#getEntity(java.lang.Long)
     */
    public YwRqQyjyxkGdyjgqjgdjb getEntity(Long gdId) {
       
        return this.ywRqQyjyxkGdyjgqjgdjbDao.get(gdId);
    }

    public void delete(YwRqQyjyxkGdyjgqjgdjb entity) {
        this.ywRqQyjyxkGdyjgqjgdjbDao.delete(entity);
        
    }

    private List<YwRqQyjyxkGdyjgqjgdjb> getList(String acceptno, Long qyid){
        String hql="from "+YwRqQyjyxkGdyjgqjgdjb.class.getName()+" where qyid='"+qyid+"' and acceptNo='"+acceptno+"'";
        List<YwRqQyjyxkGdyjgqjgdjb> list=this.ywRqQyjyxkGdyjgqjgdjbDao.find(hql);
        return list;
    }
    
    public Map impportFinalData(String acceptno, Long qyid) {
        List<YwRqQyjyxkGdyjgqjgdjb> list=this.getList(acceptno, qyid);
        for(YwRqQyjyxkGdyjgqjgdjb entity:list){
            YwRqQyjyxkFinalGdyjgqjgdjb finalEntity=new YwRqQyjyxkFinalGdyjgqjgdjb();
            finalEntity.setGfszbl(entity.getGfszbl());
            finalEntity.setQygdmc(entity.getQygdmc());
            finalEntity.setUploadDate(entity.getUploadDate());
            finalEntity.setUserName(entity.getUserName());
            finalEntity.setXh(entity.getXh());
            finalEntity.setId(null);
            finalEntity.setQyid(qyid);
            this.ywRqQyjyxkFinalGdyjgqjgdjbService.saveEntity(finalEntity);
            
        }
        return null;
    }
}
